namespace Web.Page.Rss
{
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text.RegularExpressions;
    using System.Xml;
    using System.Xml.Xsl;

    using BLF.WWW_User;
    using Model.WWW_User;
    using Wuqi.Webdiyer;

    public class GetRssUrlData : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GetRssHtml();
        }

        private void GetRssHtml()
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            if (Request["RssUrl"] != null && Request["RssUrl"].ToString().Trim() != "" && Request["RssID"] != null && Request["ShowCount"] != null)
            {
                try
                {
                    string key = Request["RssUrl"].ToString().ToLower().Trim();
                    int RssID = int.Parse(Request["RssID"].ToString());
                    int ShowCount = int.Parse(Request["ShowCount"].ToString());
                    string returnHtml;

                    string htmlError = GetGlobalResourceObject("Rss", "htmlError").ToString();
                    XmlDocument xmlDoc = (XmlDocument)Cache[key];
                    XmlNode channelNode;
                    XmlNodeList itemNodes;
                    if (xmlDoc == null)
                    {
                        try
                        {
                            xmlDoc = new XmlDocument();
                            xmlDoc.Load(key);
                            channelNode = xmlDoc.SelectNodes("/rss/channel").Item(0);
                            itemNodes = xmlDoc.SelectNodes("/rss/channel/item");
                            for (int i = 9; i < itemNodes.Count; i++)
                            {
                                channelNode.RemoveChild(itemNodes.Item(i));
                            }
                            Cache.Insert(key, xmlDoc, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);
                        }
                        catch
                        {
                            Response.Write(htmlError.Replace("#id", RssID.ToString()).Replace("#url", key).Replace("#0", key));
                            return;
                        }
                    }
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                    nsmgr.AddNamespace("content", "http://purl.org/rss/1.0/modules/content/");
                    nsmgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
                    #region

                    string html1 = GetGlobalResourceObject("Rss", "html1").ToString();
                    string html2 = GetGlobalResourceObject("Rss", "html2").ToString();
                    string html3 = GetGlobalResourceObject("Rss", "html3").ToString();
                    string play = GetGlobalResourceObject("Rss", "play").ToString();
                    string mediaLink = GetGlobalResourceObject("Rss", "mediaLink").ToString();
                    //key = HttpUtility.UrlDecode(key);
                    channelNode = xmlDoc.SelectNodes("/rss/channel").Item(0);
                    itemNodes = xmlDoc.SelectNodes("/rss/channel/item");
                    for (int i = ShowCount; i < itemNodes.Count; i++)
                    {
                        channelNode.RemoveChild(itemNodes.Item(i));
                    }

                    string rss0 = "#";
                    string rss1 = "";
                    XmlNodeList tmps = xmlDoc.SelectNodes("/rss/channel/link");
                    if (tmps.Count > 0) rss0 = tmps[0].InnerText;
                    //rss0 = HttpUtility.UrlDecode(rss0);
                    tmps = xmlDoc.SelectNodes("/rss/channel/title");
                    if (tmps.Count > 0) rss1 = tmps[0].InnerText;
                    returnHtml = html1.Replace("#id", RssID.ToString()).Replace("#url", key).Replace("#0", rss0).Replace("#1", rss1);

                    itemNodes = xmlDoc.SelectNodes("/rss/channel/item");
                    string item0 = "", item1 = "#", item2 = "", item3 = "", item4 = "";
                    bool mp3Falg = false;
                    for (int i = 0; i < itemNodes.Count; i++)
                    {
                        item0 = ""; item1 = "#"; item2 = ""; item3 = ""; item4 = ""; mp3Falg = false;
                        tmps = itemNodes.Item(i).SelectNodes("title");
                        if (tmps.Count > 0) item0 = tmps[0].InnerText;

                        tmps = itemNodes.Item(i).SelectNodes("link");
                        if (tmps.Count > 0) item1 = tmps[0].InnerText;
                        if (item1 == "#")
                        {
                            tmps = itemNodes.Item(i).SelectNodes("guid");
                            if (tmps.Count > 0) item1 = tmps[0].InnerText;
                        }
                        //item1 = HttpUtility.UrlDecode(item1);

                        tmps = itemNodes.Item(i).SelectNodes("description");
                        if (tmps.Count > 0) item2 = tmps[0].InnerText;
                        if (item2 == "")
                        {
                            tmps = itemNodes.Item(i).SelectNodes("content:encoded", nsmgr);
                            if (tmps.Count > 0) item2 = tmps[0].InnerText;
                        }

                        tmps = itemNodes.Item(i).SelectNodes("pubDate");
                        if (tmps.Count > 0) item3 = tmps[0].InnerText;
                        if (item3 == "")
                        {
                            tmps = itemNodes.Item(i).SelectNodes("dc:date", nsmgr);
                            if (tmps.Count > 0) item3 = tmps[0].InnerText;
                        }

                        tmps = itemNodes.Item(i).SelectNodes("enclosure");
                        if (tmps.Count > 0) item4 = tmps[0].Attributes["url"].Value;
                        if (item4.EndsWith(".mp3")) mp3Falg = true;

                        int t = item3.IndexOf(":");
                        if (t > 1) item3 = item3.Substring(0, t);
                        if (item0.Length < 120)
                        {
                            string a = Regex.Replace(item2, "<(.|\n)+?>", "");
                            int b = (120 - item0.Length);
                            if (a.Length < b)
                            {
                                item0 = item0 + "&nbsp;<span class=\"RssFont\">" + a + "</span>";
                            }
                            else
                            {
                                item0 = item0 + "&nbsp;<span class=\"RssFont\">" + a.Substring(0, b) + "</span>";
                            }
                        }

                        returnHtml += html2.Replace("#id", RssID.ToString()).Replace("url", key).Replace("#sort", i.ToString()).Replace("#0", item0).Replace("#1", item1).Replace("#2", item2).Replace("#3", item3);
                        if (mp3Falg)
                        {
                            returnHtml = returnHtml.Replace("#4", (play + mediaLink).Replace("#0", item4).Replace("#id", RssID.ToString()).Replace("#sort", i.ToString()));
                        }
                        else if (item4.Trim() != "")
                        {
                            returnHtml = returnHtml.Replace("#4", mediaLink.Replace("#0", item4));
                        }
                        else
                        {
                            returnHtml = returnHtml.Replace("#4", "");
                        }
                    }
                    returnHtml += html3;
                    //returnHtml = returnHtml.Replace("\n", "").Replace("\r", "").Replace("\t", "");
                    #endregion
                    Response.Write(returnHtml);
                }
                catch { }
            }
        }
    }
}